package com.um.jdy.platform.device.manger;

import com.um.jdy.common.device.entity.po.EbLog;
import com.um.jdy.common.device.entity.po.EvLog;
import com.um.jdy.common.device.entity.po.PbLog;
import com.um.jdy.common.device.service.EbLogService;
import com.um.jdy.common.device.service.PbLogService;
import com.um.jdy.common.device.service.PileService;
import com.um.springboot.starter.entity.Page;
import com.um.springboot.starter.orm.entity.Response;
import com.um.springboot.starter.orm.manager.BaseManager;
import com.um.springboot.starter.orm.params.ExportParams;
import com.um.springboot.starter.orm.params.PageParams;
import com.um.springboot.starter.utils.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.*;

/**
 * 管理类：充电枪表
 */
@Component
public class PbLogManager extends BaseManager<PbLogService> {

    @Autowired
    PileService pileService;

    /**
     * 分页查询充电枪
     *
     * @param pageParams
     * @return
     */
    public Response<Page<PbLog>> getPage(@RequestBody PageParams pageParams,String createTime) {
        Page<PbLog> page = new Page<>();
        page.setPageNo(pageParams.getPageNo());
        page.setPageSize(pageParams.getPageSize());

        page = service.getCMSViewModelPageBySqlName(page,"get_pb_log_page","get_pb_log_count", PbLog.class,pageParams.getProperties(),pageParams.getValues(),pageParams.getRangeEntitys(),pageParams.getOrderNames(),pageParams.getSortOns(),
                new Object[]{getComId(),createTime+" 00:00:00",createTime+" 23:59:59"},true);
        return Response.success(page);
    }

    @Transactional
    public Response delete(String id) {
        boolean result = service.delete(id);
        return result ? Response.success() : Response.failed();
    }

    @Transactional
    public Response deleteIds(List<String> ids) {
        boolean result = service.deleteIds(ids);
        return result ? Response.success() : Response.failed();
    }

    public Response exportPage(ExportParams exportParams,String createTime) {

        List<PbLog> list = service.getCMSViewModelListBySqlName("get_pb_log_page", PbLog.class,exportParams.getProperties(),exportParams.getValues(),exportParams.getRangeEntitys(),exportParams.getOrderNames(),exportParams.getSortOns(),
                new Object[]{getComId(),createTime+" 00:00:00",createTime+" 23:59:59",exportParams.getStart(),exportParams.getLimit()},true);

        Map mapFormat = new Hashtable();
        mapFormat.put("create_time","yyyy-MM-dd HH:mm:ss");


        String fileName = "Excel"+ com.um.springboot.starter.utils.DateUtil.getDateString(new Date(), "yyyyMMddHHmmss")+".xls";
        String path = service.getExportFilePath(fileName);
        List<String> columns = Arrays.asList("命令类型","流水号","设备编码","设备IMEI","标识符","请求数据","日志时间");

        List<String> properties = Arrays.asList("cmd","seq","code","imei","identifier","request_data","create_time");

        ExcelUtil.writeEntity(path, columns, list, properties, exportParams.getMapParam(), mapFormat, exportParams.getMapZero(),true);

        return Response.success(service.getDownFilePath(fileName));
    }

}
